package com.ruoyi.project.system.jgg.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.project.system.jgg.domain.TbBaseCustomIndexDef;
import com.ruoyi.project.system.jgg.mapper.TbBaseJggMapper;
import com.ruoyi.project.system.jgg.domain.TbBaseJgg;
import com.ruoyi.project.system.jgg.service.ITbBaseJggService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 指标分类Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-12-30
 */
@Service
public class TbBaseJggServiceImpl implements ITbBaseJggService 
{
    @Autowired
    private TbBaseJggMapper tbBaseJggMapper;

    /**
     * 查询指标分类
     * 
     * @param dimensionCd 指标分类主键
     * @return 指标分类
     */
    @Override
    public TbBaseJgg selectTbBaseJggByDimensionCd(String dimensionCd)
    {
        return tbBaseJggMapper.selectTbBaseJggByDimensionCd(dimensionCd);
    }

    /**
     * 查询指标分类列表
     * 
     * @param tbBaseJgg 指标分类
     * @return 指标分类
     */
    @Override
    public List<TbBaseJgg> selectTbBaseJggList(TbBaseJgg tbBaseJgg)
    {
        return tbBaseJggMapper.selectTbBaseJggList(tbBaseJgg);
    }

    /**
     * 新增指标分类
     * 
     * @param tbBaseJgg 指标分类
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTbBaseJgg(TbBaseJgg tbBaseJgg)
    {
        int rows = tbBaseJggMapper.insertTbBaseJgg(tbBaseJgg);
        insertTbBaseCustomIndexDef(tbBaseJgg);
        return rows;
    }

    /**
     * 修改指标分类
     * 
     * @param tbBaseJgg 指标分类
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTbBaseJgg(TbBaseJgg tbBaseJgg)
    {
        tbBaseJggMapper.deleteTbBaseCustomIndexDefByIndexTypeCd(tbBaseJgg.getDimensionCd());
        insertTbBaseCustomIndexDef(tbBaseJgg);
        return tbBaseJggMapper.updateTbBaseJgg(tbBaseJgg);
    }

    /**
     * 批量删除指标分类
     * 
     * @param dimensionCds 需要删除的指标分类主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbBaseJggByDimensionCds(String dimensionCds)
    {
        tbBaseJggMapper.deleteTbBaseCustomIndexDefByIndexTypeCds(Convert.toStrArray(dimensionCds));
        return tbBaseJggMapper.deleteTbBaseJggByDimensionCds(Convert.toStrArray(dimensionCds));
    }

    /**
     * 删除指标分类信息
     * 
     * @param dimensionCd 指标分类主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbBaseJggByDimensionCd(String dimensionCd)
    {
        tbBaseJggMapper.deleteTbBaseCustomIndexDefByIndexTypeCd(dimensionCd);
        return tbBaseJggMapper.deleteTbBaseJggByDimensionCd(dimensionCd);
    }

    /**
     * 新增自选股指标定义测试信息
     * 
     * @param tbBaseJgg 指标分类对象
     */
    public void insertTbBaseCustomIndexDef(TbBaseJgg tbBaseJgg)
    {
        List<TbBaseCustomIndexDef> tbBaseCustomIndexDefList = tbBaseJgg.getTbBaseCustomIndexDefList();
        String dimensionCd = tbBaseJgg.getDimensionCd();
        if (StringUtils.isNotNull(tbBaseCustomIndexDefList))
        {
            List<TbBaseCustomIndexDef> list = new ArrayList<TbBaseCustomIndexDef>();
            for (TbBaseCustomIndexDef tbBaseCustomIndexDef : tbBaseCustomIndexDefList)
            {
                tbBaseCustomIndexDef.setIndexTypeCd(dimensionCd);
                list.add(tbBaseCustomIndexDef);
            }
            if (list.size() > 0)
            {
                tbBaseJggMapper.batchTbBaseCustomIndexDef(list);
            }
        }
    }
}
